﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ELS.User
{
    public partial class GrantForm : MdiChildForm
    {
        PermissionController permissionController = new PermissionController();
        GroupController groupController = new GroupController();
        GrantController grantController = new GrantController();

        bool isLoadedData = false;
        Dictionary<String, int> permissionOfGroups;
        Dictionary<String, int> allPermission;

        public GrantForm()
        {
            InitializeComponent();
            isLoadedData = LoadListGroups() && LoadListPermission();
        }
        private bool LoadListPermission()
        {
            DataTable dtGroups = new DataTable();
            dtGroups = groupController.GetAllGroups();
            lbGroups.DataSource = dtGroups;
            lbGroups.DisplayMember = "GROUPNAME";
            lbGroups.ValueMember = "GROUP_ID";

            if (dtGroups.Rows.Count > 0)
            {
                return true;
            }
            else return false;
        }

        private bool LoadListGroups()
        {
            DataTable dtPermission = new DataTable();
            dtPermission = permissionController.GetAllPermission();
            ((ListBox)chkbPermission).DataSource = dtPermission;
            ((ListBox)chkbPermission).DisplayMember = "CODE";
            ((ListBox)chkbPermission).ValueMember = "ID";

            if (dtPermission.Rows.Count > 0)
            {
                return true;
            }
            else return false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string currentGroupId = lbGroups.SelectedValue.ToString();
            List<int> newPermissions = new List<int>();
            foreach (object itemChecked in chkbPermission.CheckedItems)
            {
                DataRowView castedItem = itemChecked as DataRowView;
                string comapnyName = castedItem["CODE"].ToString();
                int id = int.Parse(castedItem["ID"].ToString());
                newPermissions.Add(id);
            }

            grantController.UpdatePermissionOfGroup(currentGroupId, newPermissions);



        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            isLoadedData = LoadListGroups() && LoadListPermission();
        }

        private void lbGroups_SelectedIndexChanged(object sender, EventArgs e)
        {
            int groupId = 0;
            if (isLoadedData)
            {

                groupId = int.Parse(lbGroups.SelectedValue.ToString());
                DataTable groupInfo = groupController.SelectGroupById(groupId);
                txtGroupName.Text = groupInfo.Rows[0]["GROUPNAME"].ToString();
                txtGroupDescription.Text = groupInfo.Rows[0]["DESCRIPTION"].ToString();

                for (int j = 0; j < chkbPermission.Items.Count; j++)
                {
                    chkbPermission.SetItemChecked(j, false);

                    allPermission = permissionController.GetAllPermissionByDictionary();
                    permissionOfGroups = permissionController.GetPermissionByGroupId(groupId);

                    int i = 0;
                    foreach (KeyValuePair<string, int> pairAll in allPermission)
                    {
                        foreach (KeyValuePair<string, int> pairUser in permissionOfGroups)
                        {
                            if (pairAll.Value == pairUser.Value)
                            {
                                chkbPermission.SetItemChecked(i, true);
                            }
                        }
                        i++;
                    }
                }
            }


        }

        private void chkbPermission_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (isLoadedData)
            //{
            //    int permissionId = 0;
            //    permissionId = int.Parse(chkbPermission.SelectedValue.ToString());

            //    DataTable permissionInfo = permissionController.SelectPermissionByCode(permissionId);
            //    txtPermissionName.Text = permissionInfo.Rows[0]["PER_CD"].ToString();
            //    txtPermissionDesc.Text = permissionInfo.Rows[0]["PER_DESC"].ToString();
            //}
        }

        
    }
}